/*
 * Copyright 2008 Les Hazlewood
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.pojodm.security;

import org.pojodm.entity.Entity;
import org.pojodm.party.Party;

public class Role extends Entity {

    public static final String ROOT_ROLE_NAME = "root";
    public static final String PRIVATE_ROLE_NAME = "private";

    private String name;

    private String description;

    private Party owner;

    private boolean isPrivate = false;

    public Role() {}

    public Role( String name ) {
        this.name = name;
    }

    public Role( String name, Party owner ) {
        this.name = name;
        this.owner = owner;
    }

    public String getName() {
        return name;
    }

    public void setName( String name ) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription( String description ) {
        this.description = description;
    }

    public boolean isPrivate() {
        return isPrivate;
    }

    public void setPrivate(boolean isPrivate) {
        this.isPrivate = isPrivate;
    }

    public Party getOwner() {
        return owner;
    }

    public void setOwner( Party owner ) {
        this.owner = owner;
    }

    public boolean onEquals( Entity e ) {
        if ( e instanceof Role ) {
            Role r = (Role)e;
            return getName().equals( r.getName() ) &&
                   ( owner != null ? owner.equals( r.getOwner() ) : r.getOwner() == null );
        }

        return false;
    }

    public int hashCode() {
        int result = name.hashCode();
        result = 29 * result + ( owner != null ? owner.hashCode() : 0 );
        return result;
    }

    public StringBuffer toStringBuffer() {
        StringBuffer sb = super.toStringBuffer();
        sb.append(",name=").append(getName());
        sb.append(",description=[").append( getDescription() ).append( "]" );
        sb.append(",permissions={").append("<lazy property omitted>").append("}");
        return sb;
    }

    /**
     * Returns a shallow copy (i.e. the owner and Permission instances in the permissions
     * collection copied into a new list).  This should be fine since permission
     * objects are immutable.
     */
    @Override
    @SuppressWarnings( { "CloneDoesntDeclareCloneNotSupportedException" } )
    public Object clone() {
        Role clone = (Role)super.clone();
        clone.setName( getName() );
        clone.setDescription( getDescription() );
        clone.setOwner( getOwner() );

        return clone;
    }

}
